package com.wandousoushu.jiusen.ui.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wandousoushu.jiusen.MyApplication;
import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.ui.login.LoginActivity;
import com.wandousoushu.jiusen.util.ValidatePhoneUtil;
import com.wandousoushu.jiusen.web.http.MainHttpUtils;

import org.json.JSONObject;

public class PasswordChangerActivity extends AppCompatActivity {

    private LinearLayout backLinearLayout;
    private EditText oldPwdEditText;
    private EditText newPwdEditText;
    private EditText confirmPwdEditText;
    private TextView changePwdButton;
    private String oldPwd;
    private String newPwd;
    private String confirmPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_changer);
        backLinearLayout=findViewById(R.id.password_change_ll_back);
        backLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        InitView();
    }

    private void InitView(){
        oldPwdEditText = findViewById(R.id.password_edit_oldpwd);
        newPwdEditText = findViewById(R.id.password_edit_newpwd);
        confirmPwdEditText = findViewById(R.id.password_edit_confirm_pwd);
        changePwdButton = findViewById(R.id.password_change_btn_change);

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                oldPwd = oldPwdEditText.getText().toString().trim();
                newPwd = newPwdEditText.getText().toString().trim();
                confirmPwd = confirmPwdEditText.getText().toString().trim();
                changePwdButton.setEnabled(!TextUtils.isEmpty(oldPwd) && !TextUtils.isEmpty(newPwd) && !TextUtils.isEmpty(confirmPwd));
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        oldPwdEditText.addTextChangedListener(textWatcher);
        newPwdEditText.addTextChangedListener(textWatcher);
        confirmPwdEditText.addTextChangedListener(textWatcher);
        changePwdButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PasswordChange();
            }
        });
    }

    private void PasswordChange(){
        if (oldPwd.length() < 6 || oldPwd.length() > 16) {
            oldPwdEditText.setError(getResources().getString(R.string.invalid_password));
            oldPwdEditText.requestFocus();
            return;
        }
        if (newPwd.length() < 6 || newPwd.length() > 16) {
            newPwdEditText.setError(getResources().getString(R.string.invalid_password));
            newPwdEditText.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            confirmPwdEditText.setError(getResources().getString(R.string.register_pwd_comfirm_null));
            confirmPwdEditText.requestFocus();
            return;
        }
        if (!newPwd.equals(confirmPwd)) {
            confirmPwdEditText.setError(getResources().getString(R.string.register_pwd_confirm_error));
            confirmPwdEditText.requestFocus();
            return;
        }
        HttpPasswordChange();
    }

    private void HttpPasswordChange() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String  returnData = MainHttpUtils.getInstance().PasswordChange(oldPwd, newPwd, MyApplication.getInstance().getToken());
                    if (returnData == null) {
                        mHandle.sendEmptyMessage(1);
                        return;
                    }
                    JSONObject jsonObject=new JSONObject(returnData);
                    if(jsonObject!=null&&jsonObject.getInt("code")==1){
                        mHandle.sendEmptyMessage(0);
                    }else {
                        Message msg = Message.obtain();
                        msg.obj = jsonObject.getString("msg");
                        msg.what = 2;
                        mHandle.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler mHandle=new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.password_change_success), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.password_change_http_fail), Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}